package com.recordhistory.controller;

import com.recordhistory.enums.ResultEnum;
import com.recordhistory.model.*;
import com.recordhistory.service.*;
import com.recordhistory.utils.CheckTool;
import com.recordhistory.vo.EventOccurVo;
import com.recordhistory.vo.HistoryMainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author ：LX
 * 创建时间： 2019/6/12. 17:19
 * 地点：广州
 * 目的: 历史主表
 * 备注说明：
 */
@Controller
@RequestMapping("/main")
public class HistoryMainController {

    @Autowired
    private HistoryMainService historyMainService;
    @Autowired
    private EventOccurService eventOccurService;
    @Autowired
    private ReignTitleService reignTitleService;
    @Autowired
    private PeriodService periodService;
    @Autowired
    private HistoryImgService historyImgService;


    /**
     * 跳转到 历史页面
     * @return
     */
    @RequestMapping("/toHistory")
    public String toHistory(ModelMap modelMap){
        //查询历史数据
        List<HistoryMainVo> historyMainList = historyMainService.selectVoAllAdSort();
        if (historyMainList != null && historyMainList.size() > 0){
            for (HistoryMainVo historyMainVo : historyMainList){
                //季节
                List<EventOccurVo> eventOccurSeasonList = eventOccurService.selectEventBySeason(historyMainVo.getId());
                if (eventOccurSeasonList != null && eventOccurSeasonList.size() > 0){
                    for (EventOccurVo e : eventOccurSeasonList){
                        //如果不为空，则要进行深一步的查询
                        if (!CheckTool.checkNull(e.getImgPath())){
                            List<HistoryImg> imgList = historyImgService.selectByEventId(e.getId());
                            e.setHistoryImgList(imgList);
                        }
                    }
                    historyMainVo.setSeasonEvent(eventOccurSeasonList);
                }

                //年度
                List<EventOccurVo> eventOccurYearList = eventOccurService.selectEventByYear(historyMainVo.getId());
                if (eventOccurYearList != null && eventOccurYearList.size() > 0){
                    for (EventOccurVo e : eventOccurYearList){
                        //如果不为空，则要进行深一步的查询
                        if (!CheckTool.checkNull(e.getImgPath())){
                            List<HistoryImg> imgList = historyImgService.selectByEventId(e.getId());
                            e.setHistoryImgList(imgList);
                        }
                    }
                    historyMainVo.setYearEvent(eventOccurYearList);
                }

                //日期
                List<EventOccurVo> eventOccurDateList = eventOccurService.selectEventByDate(historyMainVo.getId());
                if (eventOccurDateList != null && eventOccurDateList.size() > 0){
                    for (EventOccurVo e : eventOccurDateList){
                        //如果不为空，则要进行深一步的查询
                        if (!CheckTool.checkNull(e.getImgPath())){
                            List<HistoryImg> imgList = historyImgService.selectByEventId(e.getId());
                            e.setHistoryImgList(imgList);
                        }
                    }
                    historyMainVo.setDateEvent(eventOccurDateList);
                }

                //年号
                List<ReignTitle> reignTitleList = reignTitleService.selecByHtistoryId(historyMainVo.getId());
                if (reignTitleList != null && reignTitleList.size() > 0){
                    historyMainVo.setReignTitleList(reignTitleList);
                }

                //历史时期
                List<Period> periods = periodService.selectPeriodByHistoryIdStart(historyMainVo.getId());
                if (periods != null && periods.size() > 0){
                    historyMainVo.setPeriodList(periods);
                }

            }

            modelMap.put("historyMainList", historyMainList);
        }

        return "history";
    }


    /**
     * 跳转到 历史主线新增
     * @return
     */
    @RequestMapping("/toAddMainHistory")
    public String toAddMainHistory(){
        return "addMainHistory";
    }


    /**
     * 跳转到 修改历史主线
     * @return
     */
    @RequestMapping("/toUpdMainHistory")
    public String toUpdMainHistory(ModelMap modelMap){
        List<HistoryMain> historyMainList = historyMainService.selectAllAdSort();
        modelMap.put("historyMainList", historyMainList);
        return "updMainHistory";
    }


    /**
     * 根据 公元纪年 搜索 历史主线
     * @param id 公元纪年ID
     * @return
     */
    @RequestMapping("/selectHistory")
    @ResponseBody
    public ResultJson selectHistory(String id){
        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "查询成功", historyMainService.selectHistoryMainById(id));
    }


    /**
     * 新增 历史主干
     * @param historyMain 历史主干data
     */
    @RequestMapping("/addHistoryMain")
    @ResponseBody
    public ResultJson addHistoryMain(HistoryMain historyMain){
        historyMainService.addHistoryMain(historyMain);

        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "添加成功", null);
    }

    /**
     * 更新 历史主干
     * @param historyMain 历史主干data
     */
    @RequestMapping("/updHistoryMain")
    @ResponseBody
    public ResultJson updHistoryMain(HistoryMain historyMain){
        historyMainService.updHistoryMain(historyMain);

        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "更新成功", null);
    }


    /**
     * 根据主键删除 历史主干
     * @param id 历史主干data
     */
    @RequestMapping("/delHistoryMain")
    @ResponseBody
    public ResultJson delHistoryMain(String id){
        historyMainService.delHistoryMain(id);

        return new ResultJson(null, ResultEnum.SUCCESS.getStatus(), "删除成功", null);
    }
}
